#include <bits/stdc++.h>
using namespace std;
int n, a[10010], ans = 0, maxn = -9999;
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	int r = n;
	for(int i = n; i >= 1; i--){
		if(a[i] == 0)
			--r;
		else
			break;
	}
	for(int i = 1; i <= r; i++)
	{
		if(maxn < a[i])
			maxn = a[i];
		else if(maxn > a[i]){
			ans += maxn - a[i];
		}
	}
	cout << ans << endl;
	return 0;
}